package com.siemens.industrial.web.mvc.service.admin;

import com.siemens.industrial.model.admin.Event;
import com.siemens.industrial.model.admin.User;
import com.siemens.industrial.model.admin.UserEventRelation;
import com.siemens.industrial.web.mvc.dao.admin.EventDao;
import com.siemens.industrial.web.mvc.dao.admin.UserEventRelationDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/10/24.
 */
@Service
public class EventService {
    @Resource
    EventDao eventDao;

    @Resource
    UserEventRelationDao userEventRelationDao;

    @Transactional
    public void saveEvent(Event event) {
        eventDao.save(event);
    }

    public List<Event> getEvents() {
        return eventDao.getAll();
    }

    @Transactional
    public void addUserEvent(Map<String, Object> data, User user) {
        userEventRelationDao.deleteAllRelation(user.getUserId());
        UserEventRelation userEventRelation = new UserEventRelation();
        String mail = data.get("email").toString();
        List<Integer> events = (List<Integer>) data.get("eventArr");
        for (Integer s : events) {
            userEventRelation.setUserId(user.getUserId());
            userEventRelation.setEventId(s.toString());
            userEventRelation.setEnabled(1);
            userEventRelation.setEmail(mail);

            userEventRelationDao.save(userEventRelation);
        }
    }

    @Transactional
    public void deleteAll(String userId) {
        userEventRelationDao.deleteAllRelation(userId);
    }

    public List<Map<String, Object>> getAll(String userId) {
        return userEventRelationDao.getAllByUserId(userId);
    }

    public List<Map<String,Object>> getEventIsSubsription(User user){
        String userId = user.getUserId();
        return eventDao.getEventIsSubscription(userId);
    }
}
